package com.fenghongzhang.day002;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private TextView click;
    private TextView popmenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        popmenu = (TextView) findViewById(R.id.popmenu);

        click = (TextView) findViewById(R.id.click);

        registerForContextMenu(click);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.greed:
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);

    }

    public void popmenu(View view) {

        //1弹出菜单
        /**
         * 1,上下文
         * 2,出现谁的下方
         */
        PopupMenu popupMenu = new PopupMenu(this, click);

        popupMenu.inflate(R.menu.menu);

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.red:
                        popmenu.setTextColor(Color.parseColor("#ff0000"));
                        break;
                    case R.id.greed:
                        /**
                         * 1,透明度
                         * 2-4, 颜色值
                         */
                        popmenu.setTextColor(Color.argb(100,0,255,0));
                        break;
                }
                return false;
            }
        });

        popupMenu.show();

    }
}
